<?php
// +--------------------------------------------------------------------------+
// | Media Gallery WKZ Plugin - Geeklog                                       |
// +--------------------------------------------------------------------------+
// | gallery_remote2.php                                                      |
// |                                                                          |
// | Protocol implementation for galleryRemote                                |
// +--------------------------------------------------------------------------+
// | Copyright (C) 2002-2010 by the following authors:                        |
// |                                                                          |
// | Mark R. Evans          mark AT glfusion DOT org                          |
// |                                                                          |
// | Based on work by Sam Revitch <samr7@cs.washington.edu> for Drupal        |
// | Based on the Gallery Remote Protocol by the Gallery project              |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | This program is free software; you can redistribute it and/or            |
// | modify it under the terms of the GNU General Public License              |
// | as published by the Free Software Foundation; either version 2           |
// | of the License, or (at your option) any later version.                   |
// |                                                                          |
// | This program is distributed in the hope that it will be useful,          |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            |
// | GNU General Public License for more details.                             |
// |                                                                          |
// | You should have received a copy of the GNU General Public License        |
// | along with this program; if not, write to the Free Software Foundation,  |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.          |
// |                                                                          |
// +--------------------------------------------------------------------------+

// NOTES:  Need to validate login each time, we can timeout you know.

require_once '../lib-common.php';

if (!in_array('mediagallery', $_PLUGINS)) {
    echo COM_refresh($_CONF['site_url'] . '/index.php');
    exit;
}

require_once $_CONF['path_system'] . 'lib-user.php';
require_once $_CONF['path_system'] . 'lib-security.php';
require_once $_CONF['path'] . 'plugins/mediagallery/include/common.php';
require_once $_CONF['path'] . 'plugins/mediagallery/include/classAlbum.php';
require_once $_CONF['path'] . 'plugins/mediagallery/include/classMedia.php';
require_once $_CONF['path'] . 'plugins/mediagallery/include/lib-upload.php';
require_once $_CONF['path'] . 'plugins/mediagallery/include/sort.php';

$_SYSTEM['nohttponly'] = 1;

/*
 * Definitions for handling the gallery remote protocol
 * See http://gallery.menalto.com/ for more info.
 */

define('GR_STAT_SUCCESS', 0);
define('GR_STAT_PROTO_MAJ_VER_INVAL', 101);
define('GR_STAT_PROTO_MIN_VER_INVAL', 102);
define('GR_STAT_PROTO_VER_FMT_INVAL', 103);
define('GR_STAT_PROTO_VER_MISSING', 104);
define('GR_STAT_PASSWD_WRONG', 201);
define('GR_STAT_LOGIN_MISSING', 202);
define('GR_STAT_UNKNOWN_CMD', 301);
define('GR_STAT_NO_ADD_PERMISSION', 401);
define('GR_STAT_NO_FILENAME', 402);
define('GR_STAT_UPLOAD_PHOTO_FAIL', 403);
define('GR_STAT_NO_WRITE_PERMISSION', 404);
define('GR_STAT_NO_CREATE_ALBUM_PERMISSION', 501);
define('GR_STAT_CREATE_ALBUM_FAILED', 502);
define('GR_STAT_MOVE_ALBUM_FAILED',503);
define('GR_STAT_ROTATE_IMAGE_FAILED',504);

define('GR_SERVER_VERSION', '2.15');


function _mg_gr_checkuser()
{
    global $_USER;

    if (!isset($_USER['uid']) || $_USER['uid'] < 2) {
        _mg_gr_finish(GR_STAT_LOGIN_MISSING,'Login session has expired','Login has expired');
    }
}

function _mg_gr_finish($code, $body = NULL, $message = NULL)
{
    static $gr_messages;

    if (!isset($gr_messages)) {
        $gr_messages = array(
            GR_STAT_SUCCESS => 'Successful',
            GR_STAT_PROTO_MAJ_VER_INVAL => 'The protocol major version the client is using is not supported.',
            GR_STAT_PROTO_MIN_VER_INVAL => 'The protocol minor version the client is using is not supported.',
            GR_STAT_PROTO_VER_FMT_INVAL => 'The format of the protocol version string the client sent in the request is invalid.',
            GR_STAT_PROTO_VER_MISSING => 'The request did not contain the required protocol_version key.',
            GR_STAT_PASSWD_WRONG => 'The password and/or username the client send in the request is invalid.',
            GR_STAT_LOGIN_MISSING => 'The client used the login command in the request but failed to include either the username or password (or both) in the request.',
            GR_STAT_UNKNOWN_CMD => 'The value of the cmd key is not valid.',
            GR_STAT_NO_ADD_PERMISSION => 'The user does not have permission to add an item to the gallery.',
            GR_STAT_NO_FILENAME => 'No filename was specified.',
            GR_STAT_UPLOAD_PHOTO_FAIL => 'The file was received, but could not be processed or added to the album.',
            GR_STAT_NO_WRITE_PERMISSION => 'No write permission to destination album.',
            GR_STAT_NO_CREATE_ALBUM_PERMISSION => 'A new album could not be created because the user does not have permission to do so.',
            GR_STAT_CREATE_ALBUM_FAILED => 'A new album could not be created, for a different reason (name conflict, missing data, permissions...).',
            GR_STAT_MOVE_ALBUM_FAILED => 'The album could not be moved.',
        );
    }
    if (!isset($message)) {
        $message = $gr_messages[$code];
        if (!isset($message)) {
            $message = 'Undefined error code';
        }
    }
    if ($code != GR_STAT_SUCCESS) {
        $msg = sprintf("Request failure: %s, %s", $code, $message);
    }
    header("Content-Type: text/plain");

    echo    "#__GR2PROTO__\n" .
             $body .
            "status=$code\n" .
            "status_text=$message\n";
    exit;
}

function _mg_gr_login($loginname, $passwd)
{
    global $_CONF, $_USER, $_TABLES;

    $retval = 'server_version='. GR_SERVER_VERSION."\n";

    if (!empty($loginname) && !empty($passwd)) {
        $status = SEC_authenticate($loginname, $passwd, $uid);
    } else {
        $status = -1;
    }

    if ($status == USER_ACCOUNT_ACTIVE) { // logged in AOK.
        DB_change($_TABLES['users'], 'pwrequestid', "NULL", 'uid', intval($uid));
        $userdata = SESS_getUserDataFromId($uid);
        $_USER=$userdata;
        $sessid = SESS_newSession($_USER['uid'], $_SERVER['REMOTE_ADDR'], $_CONF['session_cookie_timeout'], $_CONF['cookie_ip']);
        SESS_setSessionCookie($sessid, $_CONF['session_cookie_timeout'], $_CONF['cookie_session'], $_CONF['cookie_path'], $_CONF['cookiedomain'], $_CONF['cookiesecure']);
        PLG_loginUser ($_USER['uid']);
        // Now that we have users data see if their theme cookie is set.
        // If not set it
        setcookie ($_CONF['cookie_theme'], $_USER['theme'], time() + 31536000,
                   $_CONF['cookie_path'], $_CONF['cookiedomain'],
                   $_CONF['cookiesecure']);
        _mg_gr_finish(GR_STAT_SUCCESS, $retval);
    } else {
        _mg_gr_finish(GR_STAT_PASSWD_WRONG, $retval);
    }
}


function _mg_gr_fetch_albums($refnum, $check_writeable)
{
    global $_MG_CONF, $_USER;

    _mg_gr_checkuser( );

    $_MG_USERPREFS = MG_getUserPrefs();

    $retval = '';
    $nalbums = 0;
    $children = MG_getAlbumChildren(0);
    $root_album_owner_id = SEC_hasRights('mediagallery.admin');
    $nrows = count($children);
    for ($i=0; $i<$nrows; $i++) {
        $album = new mgAlbum($children[$i]);
        if ($album->access == 0) {
            continue;
        }
        $aid = $album->id;   // ??
        $aid = $nalbums + 1; // ??
        $album->gid = $aid;  // gid ?
        $retval .= 'album.name.'.$aid.'=' . $album->id ."\n";
        if ( $album->title != '' )
            $retval .= 'album.title.'.$aid.'='.$album->title."\n";
        if ( $album->description != '' )
            $retval .= 'album.summary.'.$aid.'='.$album->description."\n";

//        if ( $refnum ) {
//            $retval .= 'album.parent.'.$aid.'='.$album->gid."\n";
//        } else {
//            $retval .= 'album.parent.'.$aid.'='.$album->parent."\n";
//        }
        $retval .= 'album.parent.'.$aid.'='.$album->parent."\n";

        $retval .= 'album.resize_size.'.$aid.'='.'0'."\n";
        $maxsize = $album->max_image_width;
        if ( $album->max_image_height > $album->max_image_width ) {
            $maxsize = $album->max_image_height;
        }
        if ( isset($maxsize) && $maxsize != 0 && $maxsize != '' ) {
            $retval .= 'album.max_size.'.$aid.'='.$maxsize."\n";
        }

        if (($_MG_CONF['member_albums'] && $album->isMemberAlbum() && $album->owner_id == $_USER['uid'] && $_MG_USERPREFS['active']) ||
           ( $album->member_uploads && $album->access >= 2 ) ||
           ( $album->access >= 3 ) ||
           ( $root_album_owner_id/*SEC_hasRights('mediagallery.admin')*/ ) ) {
               $add = 'true';
        } else {
            $add = 'false';
        }
        $retval .= 'album.perms.add.'.$aid.'='. $add ."\n";
        $retval .= 'album.perms.write.'.$aid.'='.(($album->access == 3  && $_MG_USERPREFS['active'] == 1) || $root_album_owner_id || ($album->member_uploads==1) ? 'true' : 'false')."\n";

        $retval .= 'album.perms.del_item.'.$aid.'='.($album->access == 3 ? 'true' : 'false')."\n";
        $retval .= 'album.perms.del_alb.'.$aid.'='.($album->access == 3 ? 'true' : 'false')."\n";

        $create_sub = 'false';
        if (($_MG_CONF['member_albums'] &&
             $_MG_CONF['member_album_root'] == $album->id &&
             $_MG_CONF['member_create_new'] &&
             $_MG_USERPREFS['active']) || ($album->access >= 3)) {

            if (!$album->hidden || ($album->hidden && $root_album_owner_id)) {
                $create_sub = 'true';
            }
        }
        $retval .= 'album.perms.create_sub.'.$aid.'='.$create_sub."\n";
//          $retval .= 'album.info.extrafields.'.$aid."=Summary\n";
        $nalbums++;

        $subs = $album->getChildren();
        if (count($subs) > 0) {
            list($nalbums, $clist) = _mg_recurse_children($album->id, $nalbums, $check_writable, $refnum);
            $retval .= $clist;
        }
    }

    $retval .= 'album_count='.$nalbums."\n";
    $retval .= 'can_create_root='. ($root_album_owner_id ? 'yes' : 'no') ."\n";
    _mg_gr_finish(GR_STAT_SUCCESS, $retval,'Fetch albums successful.');
}

function _mg_recurse_children($album_id, $counter, $check_writable, $refnum = 0)
{
    $retval = '';

    $_MG_USERPREFS = MG_getUserPrefs();

    $children = MG_getAlbumChildren($album_id);
    $root_album_owner_id = SEC_hasRights('mediagallery.admin');
    $nrows = count($children);
    for ($i=0; $i<$nrows; $i++) {
        $album = new mgAlbum($children[$i]);
        if ( $album->access == 0 ) {
            continue;
        }
        $aid = $counter + 1;
        $album->gid = $aid; // gid ?
        $retval .= 'album.name.'.$aid.'=' . $album->id ."\n";
        if ( $album->title != '' ) {
            $retval .= 'album.title.'.$aid.'='.$album->title."\n";
        }
        if ( $album->summary != '' ) {
            $retval .= 'album.summary.'.$aid.'='.$album->description."\n";
        }

//        if ($refnum) {
//            $retval .= 'album.parent.'.$aid.'='.$MG_albums[$album->parent]->gid."\n";
//        } else {
//            $retval .= 'album.parent.'.$aid.'='.$MG_albums[$album->parent]->id."\n";
//        }
        $retval .= 'album.parent.'.$aid.'='.$album->parent."\n";

        $retval .= 'album.resize_size.'.$aid.'='.'0'."\n";
        $retval .= 'album.max_size.'.$aid.'='.'0'."\n";
        $retval .= 'album.thumb_size.'.$aid.'='.'200'."\n";
        if ( ($album->access == 3  && $_MG_USERPREFS['active'] == 1) || $root_album_owner_id || ($album->member_uploads==1) ) {
            $add = 'true';
        } else {
            $add = 'false';
        }
        $retval .= 'album.perms.add.'.$aid.'='.$add."\n";
        $retval .= 'album.perms.write.'.$aid.'='.(($album->access == 3  && $_MG_USERPREFS['active'] == 1) || $root_album_owner_id || ($album->member_uploads==1) ? 'true' : 'false')."\n";

        $retval .= 'album.perms.del_item.'.$aid.'='.($album->access == 3 ? 'true' : 'false')."\n";
        $retval .= 'album.perms.del_alb.'.$aid.'='.($album->access == 3 ? 'true' : 'false')."\n";
        $retval .= 'album.perms.create_sub.'.$aid.'='.(($album->access == 3  && $_MG_USERPREFS['active'] == 1) || $root_album_owner_id ? 'true' : 'false')."\n";
//          $retval .= 'album.info.extrafields.'.$aid."=Summary\n";
        $counter++;
        list($counter, $clist) = _mg_recurse_children( $album->id, $counter, $check_writable);
        $retval .= $clist;
    }
    return array( $counter, $retval );
}

function _mg_gr_fetch_album_images($aid, $albumstoo)
{
    global $_TABLES, $_MG_CONF, $_CONF;

    _mg_gr_checkuser( );

    $retval = '';

    $album_data = MG_getAlbumData($aid, array('album_title'));
    if (!empty($album_data['album_title'])) {
        $retval .= "album.caption=" . $album_data['album_title'] . "\n";
    }

    $sql = MG_buildMediaSql(array(
        'album_id'  => $aid,
        'where'     => "m.media_type = 0",
        'sortorder' => -1
    ));
    $result = DB_query($sql);
    $arrayCounter = 0;
    $MG_media = array();
    while ($row = DB_fetchArray($result)) {
        $MG_media[] = new Media($row, $aid);
        $arrayCounter++;
    }
    $numimages = 0;
    $msize = array();

    for ($i=0; $i<$arrayCounter; $i++) {
        $x = $i + 1;

        $imageDetail = '';

        $media_thumb = '';
        $media_thumb_file = '';
        $tmpstr = 'tn/' . $MG_media[$i]->filename[0] . '/' . $MG_media[$i]->filename;
        $ext = Media::getMediaExt($_MG_CONF['path_mediaobjects'] . $tmpstr);
        if (!empty($ext)) {
            $media_thumb = $tmpstr . $ext;
            $media_thumb_file = $_MG_CONF['path_mediaobjects'] . $media_thumb;
        }
        if ($media_thumb == '') continue;

        $imageDetail .= 'image.thumbName.' . $x . '=' . $media_thumb. "\n";
        $msize = getimagesize($media_thumb_file);
        $width = $media_size[0];
        $imageDetail .= 'image.thumb_width.' . $x . '=' . $msize['0'] . "\n";
        $imageDetail .= 'image.thumb_height.' .$x.'=' . $msize[1] ."\n";

        $media_orig = '';
        $media_orig_file = '';
        $tmpstr = 'orig/' . $MG_media[$i]->filename[0] . '/' . $MG_media[$i]->filename;
        $ext = Media::getMediaExt($_MG_CONF['path_mediaobjects'] . $tmpstr);
        if (!empty($ext)) {
            $media_orig = $tmpstr . $ext;
            $media_orig_file = $_MG_CONF['path_mediaobjects'] . $media_orig;
        }
        if ($media_orig == '') continue;

        $imageDetail .= 'image.name.' . $x . '=' . $media_orig . "\n";
        $msize = @getimagesize($media_orig_file);
        $imageDetail .= 'image.raw_width.' . $x . '=' . $msize[0] ."\n";
        $imageDetail .= 'image.raw_height.' .$x.'=' .$msize[1] ."\n";
        $imageDetail .= 'image.raw_filesize.' .$x.'=' . filesize($media_orig_file) . "\n";

        $media_disp = '';
        $media_disp_file = '';
        $tmpstr = 'disp/' . $MG_media[$i]->filename[0] . '/' . $MG_media[$i]->filename;
        $ext = Media::getMediaExt($_MG_CONF['path_mediaobjects'] . $tmpstr);
        if (!empty($ext)) {
            $media_disp = $tmpstr . $ext;
            $media_disp_file = $_MG_CONF['path_mediaobjects'] . $media_disp;
        }
        if ($media_disp == '') continue;

        $imageDetail .= 'image.resizedName.' . $x . '=' . $media_disp . "\n";
        $msize = @getimagesize($media_disp_file);
        $imageDetail .= 'image.resized_width.' . $x . '=' . $msize[0] ."\n";
        $imageDetail .= 'image.resized_height.' .$x.'=' .$msize[1] ."\n";

        if (!empty($MG_media[$i]->title)) {
            $imageDetail .= 'image.caption.' . $x.'='.$MG_media[$i]->title ."\n";
        }
        $imageDetail .= 'image.clicks.' . $x . '=' . $MG_media[$i]->views . "\n";
        if (!empty($MG_media[$i]->description )) {
            $imageDetail .= 'image.extrafield.Description.'.$x.'='.$MG_media[$i]->description ."\n";
        }
        $ctime = $MG_media[$i]->upload_time;
        $imageDetail .= 'image.capturedate.year.'.$x.'='.date('Y', $ctime)."\n";
        $imageDetail .= 'image.capturedate.mon.'.$x.'='.date('n', $ctime)."\n";
        $imageDetail .= 'image.capturedate.mday.'.$x.'='.date('j', $ctime)."\n";
        $imageDetail .= 'image.capturedate.hours.'.$x.'='.date('H', $ctime)."\n";
        $imageDetail .= 'image.capturedate.minutes.'.$x.'='.date('i', $ctime)."\n";
        $imageDetail .= 'image.capturedate.seconds.'.$x.'='.date('s', $ctime)."\n";
        $imageDetail .= 'image.hidden.'.$x.'=false'."\n";

        $retval .= $imageDetail;

        $numimages++;
    }
    $retval .= 'image_count='.$numimages."\n";
    $retval .= 'baseurl='.$_MG_CONF['mediaobjects_url'] . '/'."\n";

    _mg_gr_finish(GR_STAT_SUCCESS, $retval,'Success');
}

function _mg_gr_add_image($album_id, $caption, $description)
{
    global $_USER, $LANG_MG02;

    _mg_gr_checkuser( );

    $retval = '';

    $aid = $album_id;

    $album_data = MG_getAlbumData($aid, array('member_uploads', 'max_filesize'), true);
    if ( ($album_data['access'] != 3 && $album_data['member_uploads'] != 1) ) {
        _mg_gr_finish(GR_STAT_NO_WRITE_PERMISSION, $retval);
    }

    $filename    = $_FILES['userfile']['name'];
    $filetype    = $_FILES['userfile']['type'];
    $filesize    = $_FILES['userfile']['size'];
    $filetmp     = $_FILES['userfile']['tmp_name'];
    $error       = $_FILES['userfile']['error'];
    $caption     = $caption;
    $description = $description;

    if ( $album_data['max_filesize'] != 0 && $filesize > $album_data['max_filesize'] ) {
        COM_errorLog("MG Upload: File " . $filename . " exceeds maximum allowed filesize for this album");
        $tmpmsg = sprintf($LANG_MG02['upload_exceeds_max_filesize'], $filename);
        $statusMsg .= $tmpmsg . '<br' . XHTML . '>';
        continue;
    }

    if ($error != UPLOAD_ERR_OK) {
        switch( $error ) {
            case 1 :
                $tmpmsg = sprintf($LANG_MG02['upload_too_big'],$filename);
                $statusMsg .= $tmpmsg . '<br' . XHTML . '>';
                COM_errorLog('MediaGallery:  Error - ' .$tmpmsg);
                break;
            case 2 :
                $tmpmsg = sprintf($LANG_MG02['upload_too_big_html'], $filename);
                $statusMsg .= $tmpmsg  . '<br' . XHTML . '>';
                COM_errorLog('MediaGallery: Error - ' .$tmpmsg);
                break;
            case 3 :
                $tmpmsg = sprintf($LANG_MG02['partial_upload'], $filename);
                $statusMsg .= $tmpmsg  . '<br' . XHTML . '>';
                COM_errorLog('MediaGallery: Error - ' .$tmpmsg);
                break;
            case 4 :
                break;
            case 6 :
                $statusMsg .= $LANG_MG02['missing_tmp'] . '<br' . XHTML . '>';
                break;
            case 7 :
                $statusMsg .= $LANG_MG02['disk_fail'] . '<br' . XHTML . '>';
                break;
            default :
                $statusMsg .= $LANG_MG02['unknown_err'] . '<br' . XHTML . '>';
                break;
        }
        continue;
    }

    // check user quota -- do we have one????
    $user_quota = DB_getItem($_TABLES['mg_userprefs'], 'quota', "uid=" . intval($_USER['uid']));
    if ( $user_quota > 0 ) {
        $disk_used = MG_quotaUsage( $_USER['uid'] );
        if ( $disk_used+$filesize > $user_quota) {
            COM_errorLog("MG Upload: File " . $filename . " would exceeds the users quota");
            $tmpmsg = sprintf($LANG_MG02['upload_exceeds_quota'], $filename);
            $statusMsg .= $tmpmsg . '<br' . XHTML . '>';
            continue;
        }
    }

    $opt = array(
        'caption'     => $caption,
        'description' => $description,
        'filetype'    => $filetype,
        'thumbnail'   => $thumbnail,
    );
    list($rc, $msg) = MG_getFile($filetmp, $filename, $aid, $opt);

    MG_SortMedia( $aid );
    $statusMsg .= $filename . " " . $msg;
    if ( $rc == true ) {
        _mg_gr_finish(GR_STAT_SUCCESS);
    } else {
        _mg_gr_finish(GR_STAT_UPLOAD_PHOTO_FAIL,'',$statusMsg);
    }
}

function _mg_gr_add_album($parentaname, $albumname, $title, $descr)
{
    global $_TABLES, $_MG_CONF, $_CONF, $_USER;

    _mg_gr_checkuser( );

    require_once $_CONF['path'] . 'plugins/mediagallery/include/albumedit.php';

    if ( !isset($parentaname) || $parentaname == '') {
        $parentaname = 0;
    }

    if ($parentaname == 'rootalbum' )
        $parentaname = 0;

    $retval = '';
    $aid = MG_quickCreate($parentaname,$title,$descr);
    if ( $aid == -1 ) {
        _mg_gr_finish(GR_STAT_CREATE_ALBUM_FAILED,'','Album could not be created.');
    }

    $dc = time();
    $grname = addslashes($albumname);

    $retval .= 'album_name='.$aid."\n";
    _mg_gr_finish(GR_STAT_SUCCESS,$retval,'Album Created');
}

function _mg_gr_move_album($albname, $destaname)
{
    global $_MG_CONF, $_TABLES;

      _mg_gr_checkuser( );

    if ( !isset($destaname) || $destaname == '') {
        $destaname = 0;
    }

    if ( $destaname == 'rootalbum' ) {
        $destaname = 0;
    }

    $retval = '';
    $root_album_owner_id = SEC_hasRights('mediagallery.admin');

    $album_data = MG_getAlbumData($albname,   array('album_id'), true);
    $desta_data = MG_getAlbumData($destaname, array('album_id'), true);
    if ( ($album_data['access'] != 3 || $desta_data['access'] != 3) && !$root_album_owner_id ) {
        _mg_gr_finish(GR_STAT_NO_WRITE_PERMISSION, $retval,'No write permissions');
    }
    DB_change($_TABLES['mg_albums'], 'album_parent', intval($destaname), 'album_id', intval($albname));
    _mg_gr_finish(GR_STAT_SUCCESS, $retval, 'Album reparented');
}

// ** Main Processing

$cmd = $_POST['cmd'];
$numref = FALSE;

switch($cmd) {
    case 'login':
        _mg_gr_login($_POST['uname'], $_POST['password']);
        break;
    case 'fetch-albums':
        $numref = TRUE;
    case 'fetch-albums-prune':
        $check_writeable = ($_POST['check-writeable'] == 'yes') ? TRUE : FALSE;
        _mg_gr_fetch_albums($numref, $check_writeable);
        break;
    case 'fetch-album-images':
        $albums_too = ($_POST['albums_too'] == 'yes') ? TRUE : FALSE;
        _mg_gr_fetch_album_images($_POST['set_albumName'],$albums_too);
        break;
    case 'new-album':
        _mg_gr_add_album($_POST['set_albumName'],
                         $_POST['newAlbumName'],
                         $_POST['newAlbumTitle'],
                         $_POST['newAlbumDesc']);
        break;
    case 'move-album':
        _mg_gr_move_album($_POST['set_albumName'],
                                 $_POST['set_destalbumName']);
        break;
    case 'add-item':
        _mg_gr_add_image($_POST['set_albumName'],$_POST['caption'], $_POST['extrafield_Summary']);
        break;
    case '':
        $display = COM_startBlock('');
        $display .= 'For more information about Gallery Remote, see Gallery\'s website located at <a href="http://gallery.sourceforge.net">http://gallery.sourceforge.net</a>';
        $display .= COM_endBlock();
        $display = COM_createHTMLDocument($display);
        echo $display;
        exit;
    default:
        _mg_gr_finish(GR_STAT_UNKNOWN_CMD, '', 'Unknown command "%cmd"', array('%cmd' => theme('placeholder', $cmd)));
        break;
}
exit;
?>